// pages/homepage/homepage.js
var app = getApp()
Page({
  data: {
    hiddens: true,
    indicatorDots: true,
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 500,
    current: 1,    
    bussiness: [
       //'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
       // 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      // 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
      // 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      // 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      // 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
      // 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
      // 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg'
    ]
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    app.showShareMenus();
    var scene = decodeURIComponent(options.scene);
    if (scene != 'undefined') {
      app.globalData.referrerId = scene;
      app.getUserInfo();
    }
    if (options.id) {
      app.globalData.referrerId = options.id;
      app.getUserInfo();
    } else {
      app.getUserInfo();
    }
    var that = this;
    //yconsole.log(this);
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          imageUrl: app.globalData.imageUrl
        })
      }
    });
   
  },
  onShareAppMessage: function (res) {
    return {
      title: '酒吧订位打折兰桂坊九眼桥少陵路',
      path: '/pages/homepage/homepage?id=' + wx.getStorageSync("userid"),
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    getPersonLocation(this)
    getBackData(this)//获取普通商品
    getBackDataHot(this)//获取爆款
    getBackShoppingInfo(this)//获取商家信息
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭

  },
  switchnav: function (e) {
    this.setData({
      current: e.target.dataset.current
    })
    if (e.target.dataset.current == 1 && !this.data.currentPage1) {
      getBackData(this, 1)
    } else if (e.target.dataset.current == 2 && !this.data.currentPage2) {
      getBackData(this, 2)
    } else if (e.target.dataset.current == 3 && !this.data.currentPage3) {
      getBackData(this, 3)
    }
  },
  getSearch: function (e) {//获取搜索框的值
    if (e.detail.value != "") {
      this.setData({
        searchs: e.detail.value
      })
    }
  },
  turnToSearch: function () {
    wx.navigateTo({

      url: '../../pages/searchShops/searchShops',
    })
  }
  ,
  searchDirect: function () {//获取搜索商品
    var that = this;
    if (!this.data.searchs) {
      return
    }
    wx.request({
      url: app.globalData.allUrl + "/directAction/getDirectProduct",
      data: {
        pageNum: 1,
        pageCount: 100,
        productName: that.data.searchs
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        if (res.data) {
          that.setData({
            DirectProduct: res.data.list,
            currentPage1: false,
            currentPage2: false,
            currentPage3: false,
            current: -1,
          })

        }
      }
    })
  },
  toMoreSeller: function () {//跳转到更多商户页面
    wx.navigateTo({
      url: '../../pages/moreSeller/moreSeller'
    })
  },

  toDetail: function (e) {//跳转到详情页面
    wx.navigateTo({
      url: '/pages/directDetail/directDetail?id=' + e.target.dataset.id,
    })
  },
  navToMenus: function (e) {
    app.globalData.navToMenusType = e.target.id
    wx.switchTab({
      url: '../../pages/classification/classification',
    })
  }
})


function getBackData(that, classifys) {//从后台获取直营普通商品
  if (!classifys) {
    classifys = that.data.current
    that.setData({//储存当前页已经加载过的信息
      currentPage1: true
    })
  } else {
    if (classifys == 1) {
      that.setData({//储存当前页已经加载过的信息
        currentPage1: true
      })
    } else if (classifys == 2) {
      that.setData({//储存当前页已经加载过的信息
        currentPage2: true
      })
    } else if (classifys == 3) {
      that.setData({//储存当前页已经加载过的信息
        currentPage3: true
      })
    }
  }
  // console.log(classifys)
  wx.request({
    url: app.globalData.allUrl + "/directAction/getDirectProduct",
    data: {
      pageNum: 1,
      pageCount: 100,
      classify: 7,
      putaway: 1,
      // type:0//加载普通商品0，由于前期商品少，故注释
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      //console.log(res.data);
      if (res.data && res.data.list.length > 0) {
        if (classifys == 1) {
          that.setData({
            DirectProduct1: res.data.list,
          })
        } else if (classifys == 2) {
          that.setData({
            DirectProduct2: res.data.list,
          })
        } else if (classifys == 3) {          
          that.setData({
            DirectProduct3: res.data.list
          })
        }
      }
    }
  })
}

function getBackDataHot(that) {//从后台获取直营商品的爆款商品
  wx.request({
    url: app.globalData.allUrl + "/utilAction/selectIndexBanner",
    data: {
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      //console.log(res)
      if (res.data && res.data.length > 0) {
        var imgUrls = [];
        for (var i = 0; i < res.data.length; i++) {
          imgUrls.push({
            url: that.data.imageUrl + res.data[i].bannerImg
          })
        }
        that.setData({
          imgUrls: imgUrls
        })
      }
    }
  })
}



function getBackShoppingInfo(that) {//从后台获取商家信息2018-1-4 李刚
  wx.request({
    url: app.globalData.allUrl + "/agentAction/getSuperMarketInfoList",
    data: {
      pageNum:1,
      pageCount:8 
    },
    method: 'get', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      //console.log(res.data.list);     
      if (res.data && res.data.list.length > 0) {
        var shoppingImgUrls = [];
        for (var i = 0; i < res.data.list.length; i++) {
          shoppingImgUrls.push({
            shoppingImgId: res.data.list[i].id,  //点击商家跳转到对应商家id下面的商品页面
            shoppingImgurl: that.data.imageUrl + res.data.list[i].liveAction//商家实景图片
          });  
        }
        that.setData({
          bussiness: shoppingImgUrls
        })
      }
      //console.log(that.data.bussiness);
    }
  })
}



function getPersonLocation(thisid) {//
  var that = thisid;
  wx.getLocation({
    success: function (res) {
      wx.request({
        url: app.globalData.allUrl + "/utilAction/getmapinfo",
        data: {
          latitude: res.latitude,
          longitude: res.longitude
        },
        success: function (ops) {
          wx.setStorageSync('checekgetPersonLocation', 1)
          if (that.data.noRoot) {
            that.setData({
              hasRoot: false,
              noRoot: true
            })
          }
          //console.log(ops.data)
          var newJson = ops.data.indexOf("{")
          var nextJson = ops.data.lastIndexOf(")")
          var userJson = ops.data.substring(newJson, nextJson)
          var res = JSON.parse(userJson)
          var addressDetaile = res.result.addressComponent.province + res.result.addressComponent.city + res.result.addressComponent.district
          that.setData({
            district: res.result.addressComponent.district,
            adcode: res.result.addressComponent.adcode,
          })
          app.addressDetailes = addressDetaile;
          app.district = res.result.addressComponent.district
          app.adcode = res.result.addressComponent.adcode;
          wx.setStorageSync('adcode', res.result.addressComponent.adcode)
          // console.log(app.addressDetailes)
          //  isAgent(that)//判断该区域有无代理商
          // requestSpikr(that, res.result.addressComponent.adcode)
        }
      })
    }, fail: function () {
      wx.setStorageSync('checekgetPersonLocation', 0)
      that.setData({
        hasRoot: true,
        noRoot: false
      })
    }
  })

}